package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.*;
import com.tanhua.model.domain.mysql.*;
import com.tanhua.model.vo.NearUserVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lxb哦吼吼
 * @version 1.0
 * @description: TODO
 * @date 2021/11/20 21:28
 */

@DubboService
public class SoulApiImpl implements SoulApi {

    @Autowired
    private QuestionListMapper questionListMapper;

    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private OptionMapper optionMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public List<QuestionList> getQuestionList(Long userId) {
        List<QuestionList> questionList = questionListMapper.selectList(null);
        for (QuestionList list : questionList) {

            LambdaQueryWrapper<Questions> questionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            questionsLambdaQueryWrapper.eq(Questions::getQuestionListId, list.getId());
            List<Questions> questions = questionsMapper.selectList(questionsLambdaQueryWrapper);

            for (Questions question : questions) {
                LambdaQueryWrapper<Option> optionLambdaQueryWrapper = new LambdaQueryWrapper<>();
                optionLambdaQueryWrapper.eq(Option::getQuestionId, question.getId());
                List<Option> options = optionMapper.selectList(optionLambdaQueryWrapper);

                Option[] optionsArr = options.stream().map(e -> {
                    e.setOption(e.getContent());
                    return e;
                }).toArray(Option[]::new);

                question.setOptions(optionsArr);
            }

            Questions[] questionsArr = questions.stream().toArray(Questions[]::new);
            list.setQuestions(questionsArr);
        }
        //isLock
        LambdaQueryWrapper<Report> reportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reportLambdaQueryWrapper.eq(Report::getUserId, userId);
        List<Report> reports = reportMapper.selectList(reportLambdaQueryWrapper);
        if (reports.size() == 1) {
            //解锁中级试卷
            questionList.get(1).setIsLock(0);
            //设置报告id
            questionList.get(0).setReportId(reports.get(0).getId());
        } else if (reports.size() == 2) {
            //全解锁
            questionList.get(1).setIsLock(0);
            questionList.get(2).setIsLock(0);
            //设置报告id
            questionList.get(0).setReportId(reports.get(0).getId());
            questionList.get(1).setReportId(reports.get(1).getId());
        } else {
            //设置报告id
            questionList.get(0).setReportId(reports.get(0).getId());
            questionList.get(1).setReportId(reports.get(1).getId());
            questionList.get(2).setReportId(reports.get(2).getId());
        }
        return questionList;
    }

    @Override
    public List<Option> getOptionsById(List ids) {
        return optionMapper.selectBatchIds(ids);
    }

    @Override
    public String getReportId(Long userId, String questionId, List<String> optionIdList) {
        List<Option> optionList = getOptionsById(optionIdList);
        int scoreCount = 0;
        for (Option option : optionList) {
            Integer score = Integer.valueOf(option.getScore());
            scoreCount += score;
        }
        Report report = new Report();
        UserInfo userInfo = userInfoMapper.selectById(userId);
        report.setUserId(userInfo.getId().toString());
        report.setAvatar(userInfo.getAvatar());
        Questions questions = questionsMapper.selectById(questionId);
        report.setQuestionListId(questions.getQuestionListId());
        if (scoreCount <= 20) {
            report.setAnswerId("1");
        } else if (scoreCount <= 40) {
            report.setAnswerId("2");
        } else {
            report.setAnswerId("3");
        }
        //查询是否已有报告
        LambdaQueryWrapper<Report> reportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reportLambdaQueryWrapper.eq(Report::getQuestionListId, questions.getQuestionListId())
                .eq(Report::getUserId, userId);
        Report selectOne = reportMapper.selectOne(reportLambdaQueryWrapper);
        if (selectOne == null) {
            reportMapper.insert(report);
        } else {
            report.setId(selectOne.getId());
            reportMapper.updateById(report);
        }
        return report.getId();
    }

    @Override
    public Answer getReport(String reportId) {
        Report report = reportMapper.selectById(reportId);

        LambdaQueryWrapper<Report> reportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reportLambdaQueryWrapper.eq(Report::getQuestionListId, report.getQuestionListId())
                .eq(Report::getAnswerId, report.getAnswerId());
        List<Report> reportList = reportMapper.selectList(reportLambdaQueryWrapper);
        List<NearUserVo> userInfoList = reportList.stream().map(e -> {
            NearUserVo nearUserVo = new NearUserVo();
            nearUserVo.setId(Long.valueOf(e.getUserId()));
            nearUserVo.setAvatar(e.getAvatar());
            return nearUserVo;
        }).collect(Collectors.toList());

        Answer answerQ = answerMapper.selectById(report.getAnswerId());

        Answer answer = new Answer();
        answer.setConclusion(answerQ.getConclusion());
        answer.setCover(answerQ.getCover());

        HashMap<String, String> hashMap1 = new HashMap<>(2);
        HashMap<String, String> hashMap2 = new HashMap<>(2);
        HashMap<String, String> hashMap3 = new HashMap<>(2);
        HashMap<String, String> hashMap4 = new HashMap<>(2);
        hashMap1.put("key", "外向");
        hashMap1.put("value", answerQ.getExtrovert());
        hashMap2.put("key", "判断");
        hashMap2.put("value", answerQ.getJudge());
        hashMap3.put("key", "抽象");
        hashMap3.put("value", answerQ.getAbstraction());
        hashMap4.put("key", "理性");
        hashMap4.put("value", answerQ.getReason());
        List<Map> mapList = new ArrayList<>();
        mapList.add(hashMap1);
        mapList.add(hashMap2);
        mapList.add(hashMap3);
        mapList.add(hashMap4);

        answer.setDimensions(mapList);
        answer.setSimilarYou(userInfoList);

        return answer;
    }
}
